-- This file is hereby placed in the Public Domain
local addonName, ns = ...
local addon = ns[addonName]
local oUF = ns.oUF
local libstatusbar = LibStub('SuckLessStatusBar-1.0')

local utils = addon.utils
local units = {'player', 'target'}

local backdrop = {
   bgFile = [=[Interface\ChatFrame\ChatFrameBackground]=],
   insets = {top = -1, left = -1, bottom = -1, right = -1},
}

local function updateCombo(self, event, unit)
   if(unit == PlayerFrame.unit and unit ~= self.CPoints.unit) then
      self.CPoints.unit = unit
   end
end

local function func(self, unit)
   local target = unit == 'target'

   local texture = self:CreateTexture(nil, 'ARTWORK')
   if (target) then
      texture:SetTexture(utils.mediaPath .. 'targetFrame')
   else
      texture:SetTexture(utils.mediaPath .. 'PlayerFrame')
   end
   texture:SetWidth(512 * .8)
   texture:SetHeight(128 * .8)
   if (target) then
      texture:SetPoint('CENTER', self, -5, 1)
   else
      texture:SetPoint('CENTER', self, 5, 1)
   end
   utils.adjustCoord(target, texture)
   self.texture = texture

   local background = self.bg:CreateTexture(nil, 'BACKGROUND')
   background:SetTexture(utils.mediaPath .. 'PlayerBackground') 
   background:SetWidth(512 * .8)
   background:SetHeight(128 * .8)
   if (target) then
      background:SetPoint('CENTER', self, -5, 1)
   else
      background:SetPoint('CENTER', self, 5, 1)
   end

   utils.adjustCoord(target, background)

   local portrait = CreateFrame('PlayerModel', nil, self)
   portrait:SetFrameStrata('BACKGROUND')
   utils.setSize(portrait, 54, 54)
   if (target) then
      portrait:SetPoint('BOTTOMRIGHT', self, 'BOTTOMRIGHT', -25,-2)
   else
      portrait:SetPoint('BOTTOMLEFT', self, 'BOTTOMLEFT', 25,-2)
   end

   self.Portrait = portrait

   local hp = CreateFrame('StatusBar', nil, self)
   self.Health = hp
   do
      if(target) then
	 hp:SetPoint("TOPLEFT", self,"TOPLEFT", 13, -15) 
	 hp:SetPoint("BOTTOMRIGHT", self,"BOTTOMLEFT", 183, 15) 
      else
	 hp:SetPoint("TOPRIGHT", self,"TOPRIGHT", -13, -15) 
	 hp:SetPoint("BOTTOMLEFT", self,"BOTTOMRIGHT", -183, 15) 
      end
   end
   hp:SetFrameStrata('LOW')
   hp:SetFrameLevel(20)
   hp:SetStatusBarTexture(utils.mediaPath .. 'DDTex')
   hp:SetStatusBarColor(.2, .2, .2)
   hp.frequentUpdates = true
   hp.colorClass = true
   hp.Smooth = true
   hp.colorSmooth = true
   hp.bg = hp:CreateTexture(nil, 'BACKGROUND')
   hp.bg:SetAllPoints(hp)
   hp.bg:SetTexture(utils.mediaPath .. 'DDTex')
   hp.bg:SetVertexColor(.15,.15,.15)
   hp.bg.multiplier = .3


   local mp = CreateFrame('StatusBar', nil, self)
   self.Power = mp
   mp:SetFrameStrata('LOW')
   mp:SetFrameLevel(3)
   mp:SetStatusBarTexture(utils.mediaPath.."rothTex")
   do
      local xoffset = 36
      mp:SetPoint("CENTER", self, target and (0 - xoffset) or xoffset, 0)
      --[[local x, y = -5, -5
      if(target) then
	 mp:SetPoint("TOPLEFT", self,"TOPLEFT", 22, -8) 
	 mp:SetPoint("BOTTOMRIGHT", self,"BOTTOMLEFT", 175, 6.5)
      else
	 mp:SetPoint("TOPRIGHT", self,"TOPRIGHT", -22, -8) 
	 mp:SetPoint("BOTTOMLEFT", self,"BOTTOMRIGHT", -250, 6.5)
end]]
   end
   self.Power:SetWidth(150)
   self.Power:SetHeight(30)	
   
   if(target) then
      mp.colorClass = true
      mp.colorClassNPC = true
      mp.colorClassPet = true
   else
      mp.colorPower = true
   end

   mp.bg = mp:CreateTexture(nil, 'BORDER')
   mp.bg:SetAllPoints(mp)
   mp.bg:SetTexture(utils.mediaPath .. 'rothTex')
   mp.bg.multiplier = ns.colorMultiplier
   mp.bg:SetAlpha(0.1)  
   mp.bg:SetAllPoints(self.Power)
   mp.bg:SetTexture(utils.mediaPath.."powertex")

   local caster = CreateFrame('StatusBar', nil, self)
   caster:SetFrameStrata('LOW')
   caster:SetFrameLevel(3)
   caster:SetStatusBarTexture(utils.mediaPath.."rothTex")
   caster:SetStatusBarColor(0/255, 159/255, 233/255)
   utils.setSize(caster, 115, 10)
   self.Castbar = caster

   caster.tex = caster:CreateTexture(nil, 'ARTWORK')
   utils.setSize(caster.tex, 256 * .8, 128 * .8)
   caster.tex:SetTexture(utils.mediaPath .. 'castbar')

   caster.bg = caster:CreateTexture(nil, 'BACKGROUND')
   utils.setSize(caster.bg, 256 * .8, 64 * .8)
   caster.bg:SetAllPoints(caster.tex)
   caster.bg:SetTexture(utils.mediaPath .. 'castbarBG')



   caster.Button = CreateFrame('Frame', nil, self.Castbar)
   caster.Button:SetHeight(32)
   caster.Button:SetWidth(32)
   caster.Button:SetBackdrop(backdrop)
   caster.Button:SetBackdropColor(0, 0, 0)
   caster.Icon = self.Castbar.Button:CreateTexture(nil, 'ARTWORK')
   caster.Icon:SetAllPoints(self.Castbar.Button)
   caster.Icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
   
   caster.Text = caster:CreateFontString(nil, 'OVERLAY')
   caster.Text:SetFont(STANDARD_TEXT_FONT, 13, 'OUTLINE')
   caster.Text:SetPoint( 'LEFT', 5, 20)
   caster.Text:SetTextColor(153/255,204/255,242/255)
   caster.Text:SetJustifyH(target and "RIGHT" or  "LEFT")

   --    caster.Text = caster:CreateFontString(nil, 'OVERLAY')
   --    caster.Text:SetFont(STANDARD_TEXT_FONT, 16, 'OUTLINE')
   --    caster.Text:SetPoint('CENTER', caster, 0, 2)

   caster.Time = caster:CreateFontString(nil, 'OVERLAY')
   caster.Time:SetFont(STANDARD_TEXT_FONT, 12, 'OUTLINE')
   caster.CustomTimeText = utils.castbarTime
   caster.Time:SetPoint("LEFT", caster, 5, 0)

   self.IconBackdrop = CreateFrame('Frame', nil, self)
   self.IconBackdrop:SetPoint('TOPRIGHT', self.Castbar, 3, 3)
   self.IconBackdrop:SetPoint('BOTTOMLEFT', self.Castbar, -3, -3)

   if(target) then
      self.CPoints = {}
      for i = 1, 5 do
	 local size = 12 + 2 * (i - 1)
	 self.CPoints[i] = self.Health:CreateTexture(nil, 'OVERLAY')
	 self.CPoints[i]:SetHeight(size)
	 self.CPoints[i]:SetWidth(size)
	 self.CPoints[i]:SetTexture(utils.mediaPath.."bubbleTex" )
	 if(i==1) then
	    self.CPoints[i]:SetPoint('RIGHT',80,55)
	    self.CPoints[i]:SetVertexColor(1,1,1)
	 else
	    self.CPoints[i]:SetPoint('RIGHT', self.CPoints[i-1], 'LEFT', -3,4)
	 end
      end
      self.CPoints[3]:SetPoint('RIGHT', self.CPoints[2], 'LEFT', -3,3)
      self.CPoints[4]:SetPoint('RIGHT', self.CPoints[3], 'LEFT', -4,-2)
      self.CPoints[5]:SetPoint('RIGHT', self.CPoints[4], 'LEFT', -1,-6)
      self.CPoints[2]:SetVertexColor(1,1,.66)
      self.CPoints[3]:SetVertexColor(1,1,.33)
      self.CPoints[4]:SetVertexColor(1,.66,.33)
      self.CPoints[5]:SetVertexColor(1,.33,.33)
      self.CPoints.unit = 'player'
      self.CPoints.unit = PlayerFrame.unit
      PlayerFrame:RegisterEvent('UNIT_COMBO_POINTS', updateCombo)
   end
   
   if (target) then
      caster.tex:SetTexCoord(1,0,0,1)
      caster.bg:SetTexCoord(1,0,0,1)
      caster:SetPoint('BOTTOMLEFT', self, 'TOPLEFT', -20, 8)
      caster.tex:SetPoint('BOTTOMLEFT', caster, -60, -48)
      caster.Button:SetPoint('RIGHT', caster, 'LEFT', -3, -1)
   else 
      caster:SetPoint('BOTTOMRIGHT', self, 'TOPRIGHT', 20, 8)
      caster.tex:SetPoint('BOTTOMRIGHT', caster, 60, -48)
      caster.Button:SetPoint('LEFT', caster, 'RIGHT', 3, -1)
   end

end

addon:addLayoutElement(units, func)
addon:setSize(units, 270, 45)


addon:spawn(function()
	       local u = addon.units
	       local player = oUF:Spawn('player')
	       player:SetPoint('CENTER', -200, -320)
	       player:SetFrameLevel(100)
	       u.player = player

	       local target = oUF:Spawn('target')
	       target:SetPoint('CENTER', 200, -320)
	       target:SetFrameLevel(100)
	       u.target = target
	 end)


